<template>
  <!-- 监控事件配置修改-->
  <el-dialog
    :title="title"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <el-form ref="editForm" :model="editForm" :rules="rules" label-width="110px">
      <!--监控事件配置修改-->

      <el-row>
        <el-col :span="24">
          <el-form-item label="事件名称" prop="eventNm">
            <el-input
              v-model="editForm.eventNm"
              placeholder="事件名称"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="业务类型" prop="bussType">
            <el-input
              v-model="editForm.bussType"
              placeholder="业务类型"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="监控类型" prop="monitorType">
            <el-select
              v-model="editForm.monitorType"
              placeholder="监控类型"
              style="width: 100%"
              @change="monitorTypeChange"
            >
              <el-option
                v-for="dict in dictMap.MONITOR_TYPE"
                :key="dict.value"
                :label="dict.text"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="超时时间" prop="timeoutLimit" v-if="timeoutLimitShow">
            <el-input
              v-model="editForm.timeoutLimit"
              placeholder="超时时间"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="执行SQL" prop="executeSql">
            <el-input
              v-model="editForm.executeSql"
              placeholder="执行SQL"
              type="textarea"
              autosize
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="SQL可选条件" prop="sqlCon">
            <el-select
              v-model="editForm.sqlCondition"
              clearable
              placeholder="SQL可选条件"
              style="width: 100%"
              @change = "sqlConChange"
            >
              <el-option
                v-for="item in this.conditionOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">提 交</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { monitorConfDetail, monitorConfUpdate } from "@/api/cnaps/bizMng/monitorConf.js";
import { listPageMixin } from "@/libs/listPageMixin";

export default {
  name: "monitorConfEditDialog",
  mixins: [listPageMixin],
  components: {},
  props:{
    conditionOptions: Array
  },
  data() {
    return {
      title: "",
      // 是否显示弹出层
      openState: false,
      timeoutLimitShow: false,
      // 字段可编辑控制

      //数据字典
      searchFormDictList: ["MONITOR_CYCLE", "MONITOR_TYPE"],
      // 表单参数
      editForm: {},
      // 表单校验
      rules: {
        eventNm: [{ required: true, message: "请输入事件名称", trigger: "blur" }],
        bussType: [{ required: true, message: "请输入业务类型", trigger: "blur" }],
        monitorType: [{ required: true, message: "请选择监控类型", trigger: "blur" }],
        timeoutLimit: [{ required: false, message: "请输入超时时间", trigger: "blur" }],
        executeSql: [{ required: true, message: "请输入执行SQL", trigger: "blur" }],
        // monitorCycle: [{ required: true, message: "请选择监控周期", trigger: "blur" }],
      },
      opButtonList: () => {
        return [];
      },
    };
  },
  mounted() {},
  methods: {
    // 表单重置
    reset() {
      this.resetForm("editForm");
      this.$set(this, "editForm", {});
    },
    // 关闭回掉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.reset();
          done();
        })
        .catch((_) => {});
    },
    show(row) {
      this.editForm = row;
      this.title = "监控事件配置修改";
      this.openState = true;

      this.monitorTypeChange();
      this.monitorConfDetail(row);
    },

    monitorConfDetail: function (row) {
      monitorConfDetail(row).then((response) => {
        this.editForm = response.result.data;
      });
    },

    /** TODO提交按钮 */
    submitForm: function () {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          this.submit();
        }
      });
    },

    submit() {
      monitorConfUpdate(this.editForm).then((res) => {
        if (res.code === "S") {
          this.msgSuccess("提交成功");
          this.cancel();
        }
      });
    },

    cancel() {
      this.openState = false;
      this.reset();
      this.$emit("editComplete", true);
    },

    monitorTypeChange() {
      let monitorType = this.editForm.monitorType;
      if (monitorType == "02") {
        //超时

        this.timeoutLimitShow = true;
        this.rules.timeoutLimit = {
          required: true,
          message: "请输入超时时间",
          trigger: "blur",
        };
      } else {
        //发送失败或记账失败
        this.$set(this.editForm, "timeoutLimit", "");
        this.timeoutLimitShow = false;
        this.rules.timeoutLimit = {
          required: false,
          message: "请输入超时时间",
          trigger: "blur",
        };
      }
    },
    timeoutLimitBlur(blur) {
      //同步超时时间
      if (this.editForm.executeSql.indexOf("INTERVAL") > 0) {
        let startIndex = this.editForm.executeSql.indexOf("INTERVAL") + 8;
        let replaceLength = this.editForm.executeSql.indexOf("SECOND") - startIndex;
        this.$set(this.editForm, blur, blur === "executeSql" ? this.editForm.executeSql.substr(0, startIndex)
          + " -" + this.editForm.timeoutLimit + " " + this.editForm.executeSql.substr(startIndex + replaceLength)
          : this.editForm.executeSql.substr(startIndex , replaceLength).trim().replace("-",""));
      }
    },
    sqlConChange(){
      let sql = this.editForm.sqlCondition;
      let executeSql = this.editForm.executeSql;
      if (executeSql !== null && typeof executeSql !== "undefined") {
        this.$set(this.editForm, "executeSql", executeSql+sql);
      }else {
        this.$set(this.editForm, "executeSql", sql);
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
